package com.tony.demo.widgets;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class OilTableLine extends View {
    //    public static final int[] SWEEP_GRADIENT_COLORS = new int[]{
//            Color.GREEN, Color.GREEN, Color.GREEN, Color.BLUE, Color.BLUE, Color.RED, Color.RED, Color.RED};
    public static final int[] SWEEP_GRADIENT_COLORS = new int[]{
            Color.GREEN, Color.GREEN, Color.BLUE, Color.RED, Color.RED};
    private Context mContext;
    private Paint multiColorPain; //油表画笔
    private Paint intervalPaint;  //白色间隔画笔
    private Paint indicatorPaint; //指针画笔
    private int strokeWidth = 50;
    private int mOilSize;  //油表圆形半径
    private RectF oilRect;
    private Path indicatorPath; //转盘指针

    public OilTableLine(Context context) {
        this(context, null);
    }

    public OilTableLine(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OilTableLine(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        multiColorPain = new Paint();
        multiColorPain.setStrokeWidth(strokeWidth);
        multiColorPain.setAntiAlias(true);
//        multiColorPain.setColor(Color.RED);
        multiColorPain.setStyle(Paint.Style.STROKE);

        intervalPaint = new Paint();
        intervalPaint.setStrokeWidth(strokeWidth + 4);
        intervalPaint.setAntiAlias(true);
        intervalPaint.setColor(Color.WHITE);
        intervalPaint.setStyle(Paint.Style.STROKE);

        indicatorPaint = new Paint();
        indicatorPaint.setStrokeWidth(4);
        indicatorPaint.setStyle(Paint.Style.FILL);
        indicatorPaint.setColor(Color.BLACK);
        indicatorPaint.setAntiAlias(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mOilSize = Math.min(w, h) - strokeWidth;

        oilRect = new RectF(0, 0, mOilSize, mOilSize);

        //设置渐变
        Shader shader = new SweepGradient(mOilSize / 2, mOilSize / 2, SWEEP_GRADIENT_COLORS, null);
        multiColorPain.setShader(shader);

        int center = mOilSize / 2;
        indicatorPath = new Path();
        indicatorPath.reset();
        indicatorPath.moveTo(center, center - 15);
        indicatorPath.lineTo(center + 15, center - 15);
        indicatorPath.lineTo(center, center + 200);
        indicatorPath.lineTo(center - 15, center - 15);
        indicatorPath.close();

//        startAnimate();
    }

    /**
     * 我的这个写法会产生过度绘制, OilTableLineCopy应该也会产生过度绘制吧, 一样的原理
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(strokeWidth / 2, strokeWidth / 2);
        //绕着点(oilRect.right / 2, oilRect.bottom /2)旋转90度
        canvas.rotate(90, mOilSize / 2, mOilSize / 2);
        //该方法所话圆环实际效果不是从60度开始
        canvas.drawArc(oilRect, 60, 240, false, multiColorPain);
//        canvas.drawCircle(mOilSize / 2, mOilSize / 2, mOilSize / 2 - strokeWidth / 2, multiColorPain);
        float delta = 240 / 121f;  //60个间隔 + 61条油表刻度
        float startAngle = 60f + delta;
        for (int i = 0; i < 60; i++) {
            canvas.drawArc(oilRect, startAngle, delta, false, intervalPaint);
            startAngle += delta * 2;
        }
        canvas.restore();

        canvas.save();
        canvas.translate(strokeWidth / 2, strokeWidth / 2);
        canvas.rotate(currentRotate, mOilSize / 2, mOilSize / 2);
        canvas.drawPath(indicatorPath, indicatorPaint);
//        int center = mOilSize / 2 ;
//        canvas.drawLine(center, center, center, center + 200, indicatorPaint);
        canvas.restore();
    }

    private float currentRotate = 60;
    private int duration = 10000;

    private void startAnimate() {
        ValueAnimator animator = ValueAnimator.ofInt(100);
        animator.setDuration(duration);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                long currentPlayTime = animation.getCurrentPlayTime() % duration;
                currentRotate = 60 + 240 * (currentPlayTime / (float) duration);
                invalidate();
            }
        });
//        animator.addListener(new AnimatorListenerAdapter() {
//            @Override
//            public void onAnimationEnd(Animator animation) {
//                super.onAnimationEnd(animation);
//                animator = null;
//                startAnimate();
//            }
//        });
        animator.start();
    }
}
